package com.yefeng.usercenter.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yefeng.usercenter.domain.entity.User;
import com.yefeng.usercenter.domain.request.UserLoginRequest;
import com.yefeng.usercenter.domain.request.UserRegisterRequest;
import com.yefeng.usercenter.domain.response.BaseResponse;
import com.yefeng.usercenter.exception.BusinessException;
import com.yefeng.usercenter.exception.enums.ErrorCode;
import com.yefeng.usercenter.service.UserService;
import com.yefeng.usercenter.util.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.yefeng.usercenter.constants.UserConstants.ADMIN_ROLE;
import static com.yefeng.usercenter.constants.UserConstants.USER_LOGIN_STATUS;

/**
 * @author 叶枫
 * @date 2024/4/6.
 */

@RestController
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/user/register")
    public BaseResponse<Long> userRegister (@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
//            return ResultUtils.error(ErrorCode.NULL_ERROR);
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword,planetCode)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword,planetCode );
        return ResultUtils.success(result);
    }
    @PostMapping("/user/login")
    public BaseResponse<User> userLogin( @RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }
    @GetMapping("/user/search")
    public BaseResponse<List<User>> searchUsers(String username,HttpServletRequest request){
        User user =(User) request.getSession().getAttribute(USER_LOGIN_STATUS);
        //检查是否为管理员
        if(!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        //查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isBlank(username),"user_name",username);
        List<User> userList = userService.list(queryWrapper);
        return ResultUtils.success(userList);
    }
    @PostMapping("/user/delete")
    public BaseResponse<Boolean> deleteUser(@RequestParam Long id,
                              HttpServletRequest request){
       if (!isAdmin(request)){
           throw new BusinessException(ErrorCode.NO_AUTH);
       }
        if (id <=0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean b = userService.removeById(id);
       return ResultUtils.success(b);

    }
    @PostMapping("/user/searchUserById")
    public BaseResponse<User> searchUserById(@RequestParam Long id,
                           HttpServletRequest request){
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if (id <=0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User byId = userService.getById(id);
        return ResultUtils.success(byId);
    }

    /**
     * 获取当前用户信息
     * @param request
     * @return 当前用户
     */
    @GetMapping("/user/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request){
        //获取当前登录态
        User user =(User) request.getSession().getAttribute(USER_LOGIN_STATUS);

        if (user == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        //根据id获取当前用户
        User currentUser = userService.getById(user.getId());
        User safetyUser = userService.isSafetyUser(currentUser);
        return ResultUtils.success(safetyUser);
    }

    private boolean isAdmin(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(USER_LOGIN_STATUS);

        if (user == null || user.getUserRole() != ADMIN_ROLE){
            return false;
        }
        return true;
    }
}
